package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.ProductDTO;
import com.xbongbong.saas.service.CostOptimizeExplainService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author WuHB
 * @date 2021/12/21 18:32
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/costOptimize")
public class CostOptimizeController {

    @Resource
    private CostOptimizeExplainService costOptimizeExplainService;

    /**
     * 修复库存流水解释
     *
     * @param productDTO 脚本请求类
     * @return 影响范围
     * @throws XbbException 系统异常
     */
    @RequestMapping(value = "/repairStockFlowBillExplain", method = RequestMethod.POST)
    public String repairStockFlowBillExplain(@RequestBody @Valid ProductDTO productDTO, BindingResult bindingResult) {
        XbbResponse<String> response;
        if (bindingResult.hasErrors()) {
            String errMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String scriptVO = costOptimizeExplainService.repairStockFlowBillExplain(productDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 修复批次流水解释
     *
     * @param productDTO 脚本请求类
     * @return 影响范围
     * @throws XbbException 系统异常
     */
    @RequestMapping(value = "/repairBatchFlowBillExplain", method = RequestMethod.POST)
    public String repairBatchFlowBillExplain(@RequestBody @Valid ProductDTO productDTO, BindingResult bindingResult) {
        XbbResponse<String> response;
        if (bindingResult.hasErrors()) {
            String errMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String scriptVO = costOptimizeExplainService.repairBatchFlowBillExplain(productDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 回滚库存流水解释
     *
     * @param productDTO 脚本请求类
     * @return 影响范围
     * @throws XbbException 系统异常
     */
    @RequestMapping(value = "/rollBackStockFlowBillExplain", method = RequestMethod.POST)
    public String rollBackStockFlowBillExplain(@RequestBody @Valid ProductDTO productDTO, BindingResult bindingResult) {
        XbbResponse<String> response;
        if (bindingResult.hasErrors()) {
            String errMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String scriptVO = costOptimizeExplainService.rollBackStockFlowBillExplain(productDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 回滚批次流水解释
     *
     * @param productDTO 脚本请求类
     * @return 影响范围
     * @throws XbbException 系统异常
     */
    @RequestMapping(value = "/rollBackBatchFlowBillExplain", method = RequestMethod.POST)
    public String rollBackBatchFlowBillExplain(@RequestBody @Valid ProductDTO productDTO, BindingResult bindingResult) {
        XbbResponse<String> response;
        if (bindingResult.hasErrors()) {
            String errMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String scriptVO = costOptimizeExplainService.rollBackBatchFlowBillExplain(productDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 修改成本调整单仓库解释
     *
     * @param productDTO 脚本请求类
     * @return 影响范围
     */
    @RequestMapping(value = "/repairWarehouseExplain", method = RequestMethod.POST)
    public String repairWarehouseExplain(@RequestBody @Valid ProductDTO productDTO, BindingResult bindingResult) {
        XbbResponse<String> response;
        if (bindingResult.hasErrors()) {
            String errMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String scriptVO = costOptimizeExplainService.repairWarehouseExplain(productDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 回滚成本调整单仓库解释
     *
     * @param productDTO 脚本请求类
     * @return 影响范围
     */
    @RequestMapping(value = "/rollBackWarehouseExplain", method = RequestMethod.POST)
    public String rollBackWarehouseExplain(@RequestBody @Valid ProductDTO productDTO, BindingResult bindingResult) {
        XbbResponse<String> response;
        if (bindingResult.hasErrors()) {
            String errMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String scriptVO = costOptimizeExplainService.rollBackWarehouseExplain(productDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
